// pages/member-friend/member-friend.js
import api from '../../utils/api.js'
import { config } from '../../utils/config'
let app = getApp();
import {
    HTTP
} from '../../utils/http.js'
let http = new HTTP()
Page({
    data: {
        // 此页面 页面内容距最顶部的距离
        height: app.globalData.height * 2 + 20,
        showModel: false, // 模态窗口
        showDetail: false, //展示 规则
        friendDynamicList: [],
        authorizeBtn: false,
        imgUrl: config.imgUrl,

    },
    goRUle() {
        wx.switchTab({
            url: '/pages/member-friendRule/member-friendRule',
        })
    },

    toMemberSongCoins() {
        wx.navigateTo({
            url: '/pages/member-songCoins/member-songCoins',
        })
    },
    //好友动态！！！！页面
    goFriendDynamic() {
        wx.navigateTo({
            url: '/pages/member-friendDynamic/member-friendDynamic',
        })
    },
    //获得自己朋友圈更新列表
    getCronyDynamicList(user_id) {
        http.request({
            url: api.getCronyDynamicList,
            data: {
                id: user_id,
                page: 1,
                size: 10
            },
            success: (res) => {
                console.log('朋友圈', res);
                if (res.code == 200) {
                    res.data.map((ele, index, arr) => {
                        this.sureCronyAttention(ele.id, user_id, arr)
                    })
                    console.log('改变数据结构后，', res.data)
                    //  this.setData({
                    //    friendDynamicList:res.data
                    //  })
                } else if (res.error_code == 201) {
                    let msg = '您的朋友圈还没有动态！'
                    this.setData({
                        friendDynamicNone: msg
                    })
                }
            }
        });
    },
    //判断用户是否点赞一条留言
    sureCronyAttention(id, uid, arr) {
        http.request({
            url: api.sureCronyAttention,
            data: {
                id: id, //朋友圈ID
                uid: uid, //用户id
            },
            success: (res) => {
                console.log('判断用户是否点赞一条留言', res);
                if (res.code == 200) {
                    arr.forEach((ele) => {
                        if (ele.id == id) {
                            ele.like = true;
                        }
                    })
                    this.setData({
                        friendDynamicList: arr
                    })
                } else if (res.code == 201) {
                    arr.forEach((ele) => {
                        if (ele.id == id) {
                            ele.like = false;
                        }
                    })
                    this.setData({
                        friendDynamicList: arr
                    })
                }
            }
        });
    },
    //用户点赞或者取消点赞一条帖子
    //statusID————1点赞  2取消点赞
    addCronyAttention(e) {
        let friendDynamicList = this.data.friendDynamicList;
        let id = e.currentTarget.dataset.id;
        let uid = wx.getStorageSync('user_id');
        let statusID = e.currentTarget.dataset.statusid;
        http.request({
            url: api.addCronyAttention,
            data: {
                id, //帖子ID
                uid, //用户id
                statusID
            },
            success: (res) => {
                if (res.code == 200 && statusID == 1) {
                    console.log('朋友圈——用户点赞成功', res);
                    friendDynamicList.forEach((ele) => {
                        if (ele.id === id) {
                            ele.like = true
                            ele.attention += 1
                        }
                    })
                    this.setData({
                        friendDynamicList
                    })
                } else if (res.code == 200 && statusID == 2) {
                    console.log('朋友圈——用户取消点赞点赞成功', res);
                    friendDynamicList.forEach((ele) => {
                        if (ele.id === id) {
                            ele.like = false
                            ele.attention -= 1
                        }
                    })
                    this.setData({
                        friendDynamicList
                    })
                }
            }
        });
    },
    //拿到金币全国排名 及前三信息
    getQuanGuoPaiMin(user_id) {
        http.request({
            url: api.getQuanGuoPaiMin,
            data: {
                id: user_id,
            },
            success: (res) => {
                console.log('拿到金币全国排名', res);
                if (res.code == 200) {
                    this.setData({
                        goldRanking: res.data.quanguooaimin
                    })
                }
            }
        });
    },
    onLoad: function (options) {
        this.userShareEnter(options)
        let user_id = wx.getStorageSync('user_id');
        this.getCronyList(user_id);
        this.getQuanGuoPaiMin(user_id);
    },
    onShow: function () {
        let user_id = wx.getStorageSync('user_id');
        this.getCronyDynamicList(user_id);
    },
    //拿到好友列表
    getCronyList(user_id) {
        http.request({
            url: api.getCronyList,
            data: {
                id: user_id,
            },
            success: (res) => {
                console.log('拿到好友列表', res);
                if (res.code == 200) {
                    let arrList = this.handleFriendData(res.data);
                    let goldTotal = this.getAllGoldBi(arrList);
                    let myOwerInfo = this.getOwerInfo(arrList, user_id)
                    console.log('goldTotal', goldTotal)
                    console.log(arrList)
                    this.setData({
                        friendList: res.data,
                        arrList,
                        goldTotal,
                        myOwerInfo
                    })
                }
            }
        });
    },
    //处理好友数据，金币总和，剔除排名，合并成数组
    handleFriendData(data) {
        let arr = []
        for (let key in data) {
            console.log(key);

            if (data[key] instanceof Object) {
                arr.push(data[key])
            }
        }
        return arr;
    },
    //处理好友数据，金币总和，剔除排名，合并成数组
    handleFriendData(data) {
        let arr = []
        for (let key in data) {
            console.log(key);

            if (data[key] instanceof Object) {
                arr.push(data[key])
            }
        }
        return arr;
    },

    //拿到金歌币总和，我的 + 我的好友的
    getAllGoldBi(arr) {
        let num = 0;
        arr.forEach((ele) => {
            num += ele.new_gold_gebi_num;
        })

        return num
    },

    //拿到 自己的 信息
    getOwerInfo(arr, my_user_id) {
        let newArr = arr.filter((ele) => {
            return ele.client_id == my_user_id
        })

        return newArr;
    },
    //好友列表
    goMemberFriendList() {
        wx.navigateTo({
            url: '/pages/member-friendList/member-friendList',
        })
    },
    // 展示规则
    showDetail(e) {
        let showDetail = this.data.showDetail;
        let showModel = this.data.showModel;

        this.setData({
            showDetail: !showDetail,
            showModel: !showModel
        })

    },
    close(e) {
        this.setData({
            productDetail: [],
            details: [],
            showDetail: false,
            showModel: false
        })
    },

    onShareAppMessage(res) {
        console.log('res', res)
        if (res.from === 'menu') { } //button  menu
        var content = {
            title: '味歌，改变买菜习惯：先选菜谱后买菜！推荐生鲜店入住奖汽车！',
            imageUrl: 'https://images.weigeee.com/20190823/9ad98eac2a91b6d21b2509b7849b5c36.png',
            path: `/pages/member-friendRule/member-friendRule?typeShare=1`,
            success: function (res) {
                console.log("转发成功！");
            }
        }

        return content
    },
    userShareEnter(options) {
        //由分享进入时，别人进入你的分享时
        if (options.typeShare == 1) {
            let nickName = wx.getStorageSync('nickName');
            //若用户已授权
            if (nickName) {
                //存在，即已经获得授权
                this.setData({
                    share: false
                })
            } else {
                //没获得授权时， 显示授权按钮
                this.setData({
                    authorizeBtn: true
                })
            }
        }

        wx.updateShareMenu({
            withShareTicket: true
        })

    },
})